package com.tianqu.tms.www.financia.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TmsFinanciaDto;
import com.tianqu.tms.dto.TmsFinanciaParam;
import com.tianqu.tms.entity.TmsFinancial;
import com.tianqu.tms.www.cardect.action.CarSearchInputSubmitAction;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCompanyInputViewHelper;

public class FinanciaCompanyListController  extends TmsController {
	private Logger logger = Logger.getLogger(FinanciaCompanyListController.class);
	public static final String PAGING = "paging";
	public static final String DATA = "list";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			FinanciaCompanyInputViewHelper viewhelp = (FinanciaCompanyInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
			TmsFinancial financial = TmsFinancial.getTmsFinancialPageInstance();
			if (uc.getAttribute(CarSearchInputSubmitAction.PAGENO) != null) {
				financial.setPageNo(Integer.parseInt((String) uc.getAttribute(CarSearchInputSubmitAction.PAGENO)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.MAXINDEX) != null) {
				financial.setMaxIndex(Integer.parseInt((String) uc.getAttribute(CarSearchInputSubmitAction.MAXINDEX)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.SORTKEY) != null) {
				financial.setSortKey(Integer.parseInt((String) uc.getAttribute(CarSearchInputSubmitAction.SORTKEY)));
			}
			if (uc.getAttribute(CarSearchInputSubmitAction.ASC) != null) {
				financial.setAsc((new Boolean((String) uc.getAttribute(CarSearchInputSubmitAction.ASC))) .booleanValue());
			}
			
			
			TmsFinanciaParam param = new TmsFinanciaParam();
			if(StringUtil.isNotBlank(viewhelp.getFinanciaId())) param.setFinanciaId(viewhelp.getFinanciaId());
			if(StringUtil.isNotBlank(viewhelp.getStatus())) param.setFlag(viewhelp.getStatus());
			List<TmsFinanciaDto> list = financial.findTmsFinancia(param);
			
			uc.setAttribute(DATA, list);
			uc.setAttribute(PAGING, financial);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}

}
